<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
    <title>设置-含麻登记</title>

    <!-- Bootstrap -->
    <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <link href="./../css/comm.css" rel="stylesheet">
    <link href="../css/hempRegister.css" rel="stylesheet">
    
    <!-- HTML5 shim 和 Respond.js 是为了让 IE8 支持 HTML5 元素和媒体查询（media queries）功能 -->
    <!-- 警告：通过 file:// 协议（就是直接将 html 页面拖拽到浏览器中）访问页面时 Respond.js 不起作用 -->
    <!--[if lt IE 9]>
      <script src="https://cdn.bootcss.com/html5shiv/3.7.3/html5shiv.min.js"></script>
      <script src="https://cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  <body>
    <!-- begin header -->
    <header>
        <nav class="navbar navbar-default" >
          <ul class="nav nav-tabs nav-justified top-nav" style="width: 96%;margin: 0 auto">
            <li>
              <h2>LOGO/**处方平台</h2>
            </li>
			<li>
				<button id="chooseMember"><span>选择会员</span><span><img src="../img/icon-search.jpg" alt=""></span></button>
			</li>
            <li><p>2018年8月20号   星期一</p></li>
          </ul>
        </nav>
    </header>
    <!-- end header -->
    <!-- begin content -->
      <div class="content">
          <div class="nav-top container">
          	<div class="nav-top-content row">
          		<div class="col-md-4">
          			<label for="memberName" >姓名</label>
          			<input type="text" class="form-control" id="memberName" >
          		</div>
							<div class="col-md-4">
								<label for="memberID" >身份证</label>
								<input type="text" class="form-control" id="memberID" >
							</div>
							<div class="col-md-4">
								<label for="memberPhone" >电话</label>
								<input type="text" class="form-control" id="memberPhone" >
							</div>
          	</div>
          </div>
		  <div class="add-medicel">
			  <div class="add-medicel-content container">
				  <div class="row">
						<div class="col-md-4">
							<label for="medicalName" >药品名称</label>
							<input type="text" class="form-control" id="medicalName">
						</div>
						<div class="col-md-3">
							<label for="medicalStandard" >规格</label>
							<input type="text" class="form-control" id="medicalStandard" >
						</div>
						<div class="col-md-3">
							<label for="medicalNum" >数量</label>
							<input type="number" class="form-control" id="medicalNum" >
						</div>
						<div class="col-md-2">
							<div class="btns">
								<button id="addBtn"><i class="glyphicon glyphicon-plus"></i></button>
							</div>
						</div>
				</div>
				<div class="row">
					<div class="col-md-6">
						<ul id="medicalList"></ul>
					</div>
				</div>
				<!-- 增加的药品表格列表 -->
			  </div>
			  <div class="container">
					<div class="row " style="margin: 0">
						<div class="col-xs-6 col-md-4"></div>
						<div class="col-xs-6 col-md-4"></div>
						<div class="col-xs-6 col-md-4 form-btns">
								<button class="btn btn-default">取消</button>
								<button class="btn btn-success" id="addHemp">保存</button>
						</div>
					</div>
			  </div>
		  </div>
      </div>
	  <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
	  		<div class="modal-dialog" role="document">
	  				<div class="modal-content">
	  						<div class="modal-header">
	  							<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
	  							<h4 class="modal-title" id="myModalLabel">会员选择</h4>
	  						</div>
	  						<div class="modal-body">
	  							
	  						</div>
	  						<div class="modal-footer">
	  							<button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
	  							<button type="button" class="btn btn-primary" id="okHandle">确认</button>
	  						</div>
	  				</div>
	  		</div>
	  </div>
      <!-- end content -->
      <footer>
          <nav class="navbar navbar-default" >
          <ul class="nav nav-tabs nav-justified">
            <li >
              在线问诊
            </li>
            <li>
              在线审方
            </li>
            <li class="active">
              含麻登记
            </li>
            <li>
              会员中心
            </li>
            <li>
              设置
            </li>
          </ul>
        </nav>
      </footer>

    <!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery，所以必须放在前边) -->
    <script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
    <script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
	<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.12.1/bootstrap-table.min.js"></script>
	<script src="../js/comm.js" type="text/javascript" charset="utf-8"></script>
	<script type="text/javascript">
		  var memberColHTML="";
		  var gender="";
      var curObj;
			var curData=[];
			/* 药品列表 */
			var medicalList=[];
			var personForm;
			var medicalForm;
			/* 填入的用户信息 */
			var hadAddMedicals=[];
			$('.nav-top').css({'display':'none'});
		$(function(){
			/* 查询会员列表 */
			$.asyncAx(
				  '/storeAssociator/queryMember',
				  {
				  	limit: 10,
				  	offset: 0,
				  },
				  null,
				  null,
				  null,
				  function(data){
				      curdata=data.rows;
				      for (var i=0;i<curdata.length;i++){
				          if(curdata[i].Sex==0){
							  gender='女'
						  }else{
				              gender='男'
						  }
                  memberColHTML+='<tr>'+
											'<td>'+curdata[i].associatorId+'</td>'+
                      '<td>'+curdata[i].associatorName+'</td>'+
                      '<td>'+gender+'</td>'+
                      '<td>'+curdata[i].phone+'</td>'+
                      '<td>'+curdata[i].personId+'</td>'+
                      '</tr>';
					  }
				  },
				  function(){

				  }
			  );
			
			$('#chooseMember').click(function(){
				openMdal($('#myModal'),'选择会员');
				var modalBodyHTML='<div>'+
									'<div class="row search-div">'+
										'<div class="col-md-5">'+
											'<label for="memberName">电话</label>'+
											'<input type="text" class="form-control" id="memberName" >'+
										'</div>'+
										'<div class="col-md-5">'+
											'<label for="memberName">身份证</label>'+
											'<input type="text" class="form-control" id="memberID" >'+
										'</div>'+
										'<div class="col-md-2">'+
											'<button class="btn btn-default">查询</button>'+
										'</div>'+
									'</div>'+
									'<table class="table roles-table" id="member-table">'+
										'<thead>'+
											'<tr>'+
												'<td>姓名</td>'+
												'<td>性别</td>'+
												'<td>年龄</td>'+
												'<td>电话</td>'+
												'<td>身份证</td>'+
											'</tr>'+
										'</thead>'+
										'<tbody>'+memberColHTML+'</tbody>'+
									'</table>'+
									'</div>';
				$('.modal-body').html(modalBodyHTML);
			})
			/* 点击表格列表选择会员 */
            $('body').on('click','.roles-table',function(e){
                curObj=getObj(e).parentNode;
								var curIndex=curObj.childNodes[0].innerHTML;
								// $('.roles-table tr').css({'backgroundColor':'#fff','color':'#333'});
                $(curObj).addClass('trActive');
								for (var i=0;i<curdata.length;i++) {
									if(curdata[i].associatorId==curIndex){
										personForm=curdata[i];
									}
								}
			})
            /*确认选择会员*/
            $('body').on('click','#okHandle',function(){
							$('#myModal').modal('hide');
							if(personForm!==undefined){
								$('.nav-top').css({'display':'block'});
								$('#memberName').val(personForm.associatorName);
								$('#memberID').val(personForm.personId);
								$('#memberPhone').val(personForm.phone);
								$('#myModalLabel').html(personForm.associatorName)
							}  
           })
			/* 查询药品列表 */
			function getMedicalList(name){
				$.asyncAx(
					'/web/HempRegiste/queryBaseDrugDto',
					{
						Drug_Name:name
					},
					null,
					null,
					null,
					function(data){
						medicalList=data.obj;
						for (var i=0;i<data.obj.length;i++) {
							$('#medicalList').css('display','block');
							$('#medicalList').html('<li index='+data.obj[i].drug_ID+'><span>'+data.obj[i].drug_Name+'</span><span style="padding-left:30px">'+data.obj[i].spec+'</span></li>')
						}  
					},
					function(){
					}
				);
			}
			$('#medicalName').keyup(function(){
				getMedicalList($(this).val())
			})
			/* 选择药品后，将药品放入表单中 */
			$('body').on('click','#medicalList li',function(){
					var dataJson={
						 drugId:$(this).attr('index'),
						 drugName:this.childNodes[0].innerHTML,
						 spec:this.childNodes[1].innerHTML
					};
					medicalForm=dataJson;
					$('#medicalName').val(dataJson.drugName);
					$('#medicalStandard').val(dataJson.spec);
					$('#medicalList').css({'display':'none'});
			})
			/* 添加药品 */
			$('#addBtn').click(function(){
				var medicalHTML='';
				if($('#medicalName').val()!==null&&$('#medicalStandard').val()!==null&&$('#medicalNum').val()!==null){
					if(isCover(medicalForm.drugId)){
							medicalHTML+='<div class="add-medicel-content1 container medi'+medicalForm.drugId+'" curIndex='+medicalForm.drugId+'>'+
															'<div class="row">'+
															'<div class="col-md-4">'+
																'<label for="medicalName" >药品名称</label>'+
																'<input type="text" class="form-control" id="medicalName" disabled="disabled" value="'+$('#medicalName').val()+'">'+
															'</div>'+
															'<div class="col-md-3">'+
																'<label for="medicalStandard" >规格</label>'+
																'<input type="text" class="form-control" id="medicalStandard" disabled="disabled" value="'+$('#medicalStandard').val()+'">'+
															'</div>'+
															'<div class="col-md-3">'+
																'<label for="medicalNum" >数量</label>'+
																'<input type="number" class="form-control" id="medicalNum" disabled="disabled" value="'+$('#medicalNum').val()+'">'+
															'</div>'+
															'<div class="col-md-2">'+
																'<div class="btns">'+
																	'<button id="delMedicalBtn" index='+medicalForm.drugId+'><i class="glyphicon glyphicon-remove"></i></button>'+
																'</div>'+
															'</div>'+
														'</div>';
							$('.add-medicel-content').after(medicalHTML);
						}else{
							alert('药品已被添加!');
						}
				}
			})
			/* 判断药品是否重复 不能添加相同的药品 */
			function isCover(id){
				console.log(id)
				var medicelForm=$('.add-medicel-content1');
				if(medicelForm.length==0){
					return true;
				}
				for (var i=0;i<medicelForm.length;i++) {
					if(medicelForm[i].attributes[1].nodeValue==id){
						return false;
					}
					else{
						return true;
					}
				}
			}
			/* 删除药品行数据 */
			function delMedical(id){
				var medicelForm=$('.add-medicel-content1');
				for (var i=0;i<medicelForm.length;i++) {
					if(medicelForm[i].attributes[1].nodeValue==id){
						$('.add-medicel-content1').remove('.medi'+id);
						medicalList.pop(
						medicelForm[i].attributes[1].nodeValue+"///"+
						medicelForm[i].childNodes[0].childNodes[0].childNodes[1].defaultValue+
						"///"+medicelForm[i].childNodes[0].childNodes[1].childNodes[1].defaultValue+
						"///"+medicelForm[i].childNodes[0].childNodes[2].childNodes[1].defaultValue);
					}
				}
			}
			$('body').on('click','#delMedicalBtn',function(){
				var index=$(this).attr('index');
				delMedical(index);
			})
			/* 增加含麻登记记录 */
			$('#addHemp').click(function(){
				var medicelForm=$('.add-medicel-content1');
				medicalList=[];
				for (var i=0;i<medicelForm.length;i++) {
					medicalList.push(
					medicelForm[i].attributes[1].nodeValue+"///"+
					medicelForm[i].childNodes[0].childNodes[0].childNodes[1].defaultValue+
					"///"+medicelForm[i].childNodes[0].childNodes[1].childNodes[1].defaultValue+
					"///"+medicelForm[i].childNodes[0].childNodes[2].childNodes[1].defaultValue);
				}
				if(personForm==undefined){
					alert('还未选择人员');
				}else{
					var dataJson={
						businessCode:'Hemp_Registe',
						hempRegisteId:personForm.associatorId,
						parameter:medicalList
					}
					$.asyncAx(
						'/web/HempRegiste/insertHemp',
						dataJson,
						null,
						null,
						null,
						function(data){
							$('#medicalStandard').val('');
							$('#medicalName').val('');
							$('#medicalNum').val('');
						},
						function(){
						}
					);
				}
			})
		})
	</script>
  </body>
</html>